package com.tx.blog.user.service.impl;

import com.tx.blog.user.dao.IUserDao;
import com.tx.blog.user.service.IUserService;
import com.tx.blog.user.vo.UserVo;
import com.tx.blog.util.IdGenerator;
import com.tx.blog.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tx
 * @description  
 * @date  2019/4/29 14:14
 * @param  * @Param: null
 * @return 
**/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao userDao;

    @Override
    public UserVo findUserByUserName(UserVo user) {
        return userDao.findUserByUserName(user);
    }

    @Override
    public boolean registerUser(UserVo user) {
        UserVo dbUser = userDao.findUserByUserName(user);
        if(dbUser != null){
            return false;
        }
        Long userId = IdGenerator.get().nextId();
        String salt = Md5Util.getRandomSalt();
        String password = Md5Util.convertToDBPass(user.getPassword(),salt);

        user.setPassword(password);
        user.setSalt(salt);
        user.setUserId(userId+"");
        userDao.registerUser(user);
        return true;
    }

    @Override
    public UserVo findUserByUserId(String userId) {
        return userDao.findUserByUserId(userId);
    }

    @Override
    public List<UserVo> findUsers() {
        return userDao.findUsers();
    }
}
